<?php

  if (!function_exists("last_mod")) {
	function convert($size) {
		$unit=array('b','kb','mb','gb','tb','pb');
		return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
	}
  }

  if (!function_exists("last_mod")) {
	function last_mod( $file ){
		$root_path = realpath(".");
		$modtime = filemtime($root_path . "/" . $file);
		return $modtime;
	}	
  }

  if (!function_exists("submicrotime")) {
	function submicrotime($t="0.0", $teliti=8) {
		$t = (string)$t;
		return (float)substr($t, 0, ($teliti+stripos($t, ".")+1) );
	}
  }

  function show_code($source, $lang) {
	$ci=& get_instance();
	$ci->load->library("geshi");
	$ci->geshi->set_source($source);
	$ci->geshi->set_language($lang);

	return $ci->geshi->parse_code();
  }

  // set of csv output
  // in imasih defect aka cacat
  function csv_show_array($array, $header_row = false, $col_sep = ",", $row_sep = "\n", $qut = '"') {
	if (!is_array($array) or (isset($array[0]) && !is_array($array[0])) ) return false;
	
	//Header row.
	if ($header_row)
	{
		foreach ($array[0] as $key => $val)
		{
			//Escaping quotes.
			$key = str_replace($qut, "$qut$qut", $key);
			$output .= "$col_sep$qut$key$qut";
		}
		$output = substr($output, 1)."\n";
	}
	//Data rows.
	foreach ($array as $key => $val)
	{
		$tmp = '';
		foreach ($val as $cell_key => $cell_val)
		{
			//Escaping quotes.
			$cell_val = str_replace($qut, "$qut$qut", $cell_val);
			$tmp .= "$col_sep$qut$cell_val$qut";
		}
		$output .= substr($tmp, 1).$row_sep;
	}
	
	return $output;
  }
  // set of csv output
  
  
  // set of xml output
  function generate_xml_from_array($array, $node_name) {
	$xml = '';

	if (is_array($array) || is_object($array)) {
		foreach ($array as $key=>$value) {
			if (is_numeric($key)) {
				$key = $node_name;
			}
			$keynote = $key;
			$key = str_replace("#","_",str_replace(" ","_",$key));
			$xml .= "<" . $key . " label='".$keynote."'>";
			$xml .= generate_xml_from_array($value, $node_name);
			$xml .= "</" . $key . ">". "\n";
		}
	} else {
		$xml = utf8_encode(htmlspecialchars( $array, ENT_QUOTES ));
	}
	return $xml;
  }

  function array_to_xml($array, $node_block='nodes', $node_name='node') {
	$tmp = array();
	foreach($array["response"] as $dev => $cnt){
		$dmy = array("name" => $dev, "hit" => $cnt);
		$tmp[] = $dmy;
	}
	$array["response"] = $tmp;
	$array = unicode_escape_sequences( $array );
	
	$xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
	$xml .= "<$node_block>";
	$xml .= generate_xml_from_array($array, $node_name);
	$xml .= "</$node_block>\n";
	
	return $xml;
  }
  // set of xml output

  
  
  // == set of html output ==
  function html_show_array($array){	
	// do escape at once on entire $array
	$array = unicode_escape_sequences($array);
	
	$out  = "<style>.mytb td{min-width:120px}</style>";
	$out .= "<table class=\"mytb\" cellspacing=\"0\" border=\"2\">\n";
	$out .= show_array($array, 1, 0);
	$out .= "</table>\n";
	return $out;
  }

  function show_array($array, $level, $sub){
    $out = "";
    if (is_array($array) == 1){          // check if input is an array
       foreach($array as $key_val => $value) {
           $offset = "";
           if (is_array($value) == 1){   // array is multidimensional
           $out .= "<tr>";
           $offset = do_offset($level);
           $out .= $offset . "<td>" . $key_val . "</td>";
           $out .= show_array($value, $level+1, 1);
           }
           else{                        // (sub)array is not multidim
           if ($sub != 1){          // first entry for subarray
               $out .= "<tr nosub>";
               $offset = do_offset($level);
           }
           $sub = 0;
           $out .= $offset . "<td main ".$sub.">" . $key_val .
               '</td><td>' . $value . "</td>"; 
           $out .= "</tr>\n";
           }
       } //foreach $array
    }
    return $out;
  }

  function do_offset($level){
	$offset = "";
	for ($i=1; $i<$level;$i++){
		$offset = $offset . "<td></td>";
	}
	return $offset;
  }
  // set of html output
  
  
  // aditional doangan
  function unicode_escape_sequences($str){
	$working = json_encode((array)$str);
	$working = preg_replace('/\\\u([0-9a-z]{4})/', '&#x$1;', $working);
	$anu = json_decode($working, 1);
	return $anu;
  }
  
  
  
